﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp.Tools.Utils;

namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// UC_StaffInformation.xaml 的交互逻辑
    /// </summary>
    public partial class UC_StaffInformation : UserControl
    {
        public UC_StaffInformation()
        {
            InitializeComponent();
        }
        BLL.UCStaffInformation.UC_StaffInformationClient myStaff = new BLL.UCStaffInformation.UC_StaffInformationClient();
        /// <summary>
        /// 页面加载时查询出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
            #region 禁用按钮
            btnUpdate.IsEnabled = false;//禁用修改按钮
            btnUpdate.Opacity = 0.5;
            btnDelete.IsEnabled = false;//禁用删除按钮
            btnDelete.Opacity = 0.5;
            #endregion

        }
        /// <summary>
        /// 绑定表格数据
        /// </summary>
        public void GetData()
        {
            DataTable dt = myStaff.SelectAllStaff().Tables[0];
            dgPager.ShowPagesT(dgvStaff, dt, 5);
            //dgvStaff.ItemsSource = dt.DefaultView;
        }
        /// <summary>
        /// 行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvStaff_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
        /// <summary>
        /// 打开新增窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = myStaff.SelectAllStaff().Tables[0];
            //取出第一条数据单号(YG0008)
            string str = dt.Rows[0]["staffNumber"].ToString().Trim();
            //从右边开始取4个字符(6-4=2)
            int intNumber = Convert.ToInt32(str.Substring(str.Length - 4));

            WD_InsertStaffInformation myInsertStaffInformation = new WD_InsertStaffInformation(intNumber);
            myInsertStaffInformation.ShowDialog();
            GetData();
        }
        /// <summary>
        /// 打开修改窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1.选中数据                
                //获取行
                DataRowView dv = (DataRowView)dgvStaff.SelectedItem;
                //获取单元格
                int staffId = Convert.ToInt32(dv.Row["staffID"]);
                if (staffId != 0)
                {
                    WD_UpdateStaffInformation myUpdateStaffInformation = new WD_UpdateStaffInformation(dv);
                    myUpdateStaffInformation.ShowDialog();
                    GetData();
                }
                else
                {
                    MessageBox.Show("请选择要修改的行!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                }
            }
            catch (Exception)
            {
                return;
            }
           
        }
        /// <summary>
        /// 删除员工信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult dr = MessageBox.Show("是否删除？", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
                //弹出确定对话框
                if (dr == MessageBoxResult.OK)
                {
                    ////1、获取选中行的数据
                    int staffID = Convert.ToInt32(((DataRowView)dgvStaff.SelectedItem).Row["staffID"]);
                    //提取图片名字 
                   string strPicture = ((DataRowView)dgvStaff.SelectedItem).Row["staffPicture"].ToString().Trim();

                    if (staffID!=0 && strPicture != string.Empty)
                    {
                        //2、执行删除操作
                        int intCount = myStaff.DeleteStaffInformation(staffID, strPicture);
                        if (intCount > 0)
                        {
                            MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                            img_photo.Source = null;
                            GetData();//刷新页面表格数据
                        }
                        else
                        {
                            MessageBox.Show("删除失败!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    else if (staffID != 0 && strPicture == string.Empty)
                    {
                        //2、执行删除操作
                        int intCount = myStaff.DeleteStaffInformation(staffID, null);
                        if (intCount > 0)
                        {
                            MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                            GetData();//刷新页面表格数据
                        }
                        else
                        {
                            MessageBox.Show("删除失败", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择要删除的行!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                    }

                }

            }
            catch (Exception)
            {
                return;
            }
        }

        private void btnSJPrint_Click(object sender, RoutedEventArgs e)
        {

        }
        /// <summary>
        /// 打印工作证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1.选中数据                
                //获取行
                DataRowView dv = (DataRowView)dgvStaff.SelectedItem;
                //获取单元格
                int staffID = Convert.ToInt32(dv.Row["staffID"]);
                if (staffID != 0)
                {
                    //2.弹出窗口
                    WD_PrintStaff myPrintStaff = new WD_PrintStaff(dv);
                    myPrintStaff.ShowDialog();

                }
                else
                {
                    MessageBox.Show("请选择要打印的数据!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                }

            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 打印表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrintTable_Click(object sender, RoutedEventArgs e)
        {
            if (dgvStaff.Items.Count > 0)
            {
                //2.弹出窗口
                WD_PrintStaffTable myPrintStaff = new WD_PrintStaffTable();
                myPrintStaff.ShowDialog();
            }
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExcel_Click(object sender, RoutedEventArgs e)
        {
            if (ExporTotExcel.Export(dgvStaff, "员工信息") == true)
            {
                MessageBox.Show("数据导出成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("数据导出失败!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
       
     
        /// <summary>
        /// 选择员工信息(绑定图片)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvStaff_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {

            try
            {
                //获取选中行
                if ((DataRowView)dgvStaff.SelectedItem != null)
                {
                    btnUpdate.IsEnabled = true;
                    btnUpdate.Opacity = 1;
                    btnDelete.IsEnabled = true;
                    btnDelete.Opacity = 1;

                    //提取文件名称
                    string strPictureName = ((DataRowView)dgvStaff.SelectedItem).Row["staffPicture"].ToString().Trim();
                    string strPath = null;
                    if (strPictureName != string.Empty)
                    {
                        //获取文件路径
                        strPath = myStaff.SelectPhoro(strPictureName).Trim();
                    }
                    else
                    {
                        strPath = null;
                    }
                    if (strPath != null)
                    {
                        //加载图片
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        //增加这一行（指定位图图像如何利用内存缓存=在加载时将整个图像缓存到内存中。对图像数据的所有请求将通过内存存储区进行填充。）
                        bi.CacheOption = BitmapCacheOption.OnLoad;
                        bi.UriSource = new Uri(strPath);
                        bi.EndInit();
                        //获取内存图片
                        img_photo.Source = bi;
                    }
                    else
                    {
                        img_photo.Source = null;
                    }
                }
                else
                {
                    btnUpdate.IsEnabled = false;
                    btnUpdate.Opacity = 0.5;
                    btnDelete.IsEnabled = false;
                    btnDelete.Opacity = 0.5;
                }
                ////获取选中行
                //if ((DataRowView)dgvStaff.SelectedItem != null)
                //{
                    
                //}

            }
            catch (Exception)
            {
                return;
            }

        }
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Select_KeyDown(object sender, RoutedEventArgs e)
        {
            try
            {
                string select = "";
                string strContent = txt_Select.Text.Trim();
                if (strContent != "")
                {
                    select += "staffName like '%" + strContent + "%'" +
                            "or staffNumber like '%" + strContent + "%'" +
                            "or EmployeeType like '%" + strContent + "%'" +
                            "or phoneNumber like '%" + strContent + "%'";
                }

                System.Data.DataTable dtselect = myStaff.SelectAllStaff().Tables[0];
                DataView dv = new DataView(dtselect);
                System.Data.DataTable dt = new System.Data.DataTable();
                if (select != null)
                {
                    //筛选数据
                    dv.RowFilter = select;
                    dt = dv.ToTable();
                }
                if (select == "")
                {
                    //查询全部数据
                    dt = dv.ToTable();
                }
                dgvStaff.ItemsSource = dt.DefaultView;
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
